<?php

namespace app\common\model;

use think\Model;
use think\facade\Db;

class Statistics extends Model
{
    protected $name = 'statistics';


    public static function _update($amount, $key): void
    {
        $info = parent::where('key', $key)->find();
        $info->today = $info->today + $amount;
        $info->month = $info->month + $amount;
        $info->year = $info->year + $amount;
        $info->sum = $info->sum + $amount;
        $info->save();
    }

    /**
     * 原点 2021年12月31日15:45:30 定时更新统计，需配合定时任务使用
     */
    public static function _timing(): void
    {
        $m = date('m');
        $d = date('d');
        if ($m == '01' && $d == '01') {
            Db::execute('update `statistics` set yesterday = today,last_month = `month`,today = 0,`month` = 0,`year` = 0');
        } elseif ($d == '01') {
            Db::execute('update `statistics` set yesterday = today,last_month = `month`,today = 0,`month` = 0');
        } else {
            Db::execute('update `statistics` set yesterday = today,today = 0');
        }
    }

    /**
     * 此处为演示，是否启用，根据项目而定
     */
    public static function getUserCensus($param = '')
    {
        return [['title' => '总会员', 'value' => 100]];
    }
}